//
//  PlistFileManage.m
//  IPCamera
//
//  Created by 宣佚 on 15/6/3.
//  Copyright (c) 2015年 Andon Health Co,.Ltd. All rights reserved.
//

#import "PlistFileManage.h"

@implementation PlistFileManage

+(instancetype)sharedHandler
{
    static PlistFileManage *_sharedHandler = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedHandler = [[PlistFileManage alloc] init];
    });
    return _sharedHandler;
}

-(void)MoveInitTutkPlistToSandbox
{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"TutkDeviceList" ofType:@"plist"];
    NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath1 = [paths objectAtIndex:0];
    
    NSString *filename=[plistPath1 stringByAppendingPathComponent:@"TutkDeviceList.plist"];
    [data writeToFile:filename atomically:YES];
}

-(void)MoveInitGoolinkPlistToSandbox
{
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"GoolinkDeviceList" ofType:@"plist"];
    NSMutableArray *data = [[NSMutableArray alloc] initWithContentsOfFile:plistPath];
    
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath1 = [paths objectAtIndex:0];
    
    NSString *filename=[plistPath1 stringByAppendingPathComponent:@"GoolinkDeviceList.plist"];
    [data writeToFile:filename atomically:YES];
}

-(void)initSandboxDeviceList
{
    [self MoveInitTutkPlistToSandbox];
    [self MoveInitGoolinkPlistToSandbox];
}

-(BOOL)refreshTutkDeviceList:(NSArray *)data
{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath1 = [paths objectAtIndex:0];
    
    NSString *filename=[plistPath1 stringByAppendingPathComponent:@"TutkDeviceList.plist"];
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:filename]) {
        [fm removeItemAtPath:filename error:nil];
    }
    return [data writeToFile:filename atomically:YES];
}

-(BOOL)refreshGoolinkDeviceList:(NSArray *)data
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath1 = [paths objectAtIndex:0];
    
    NSString *filename = [plistPath1 stringByAppendingPathComponent:@"GoolinkDeviceList.plist"];
    NSFileManager *fm = [NSFileManager defaultManager];
    if ([fm fileExistsAtPath:filename]) {
        [fm removeItemAtPath:filename error:nil];
    }
    return [data writeToFile:filename atomically:YES];
}

-(BOOL)refreshDeviceList:(NSArray *)data linkType:(ENUM_LINKTYPE)linkType
{
    switch (linkType) {
        case LINKTYPE_TUTK:
            return [self refreshTutkDeviceList:data];
            break;
        case LINKTYPE_GOOLINK:
            return [self refreshGoolinkDeviceList:data];
            break;
        default:
            return NO;
            break;
    }
}

-(NSMutableArray *)readDeviceList:(ENUM_LINKTYPE)linkType
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *plistPath1 = [paths objectAtIndex:0];
    
    NSString *filename = @"";
    NSMutableArray *list = [[NSMutableArray alloc] initWithCapacity:1];
    switch (linkType) {
        case LINKTYPE_TUTK:
            filename = [plistPath1 stringByAppendingPathComponent:@"TutkDeviceList.plist"];
            list = [NSMutableArray arrayWithContentsOfFile:filename];
            break;
        case LINKTYPE_GOOLINK:
            filename = [plistPath1 stringByAppendingPathComponent:@"GoolinkDeviceList.plist"];
            list = [NSMutableArray arrayWithContentsOfFile:filename];
            break;
        default:
        {
            NSString *tfilename = [plistPath1 stringByAppendingPathComponent:@"TutkDeviceList.plist"];
            NSMutableArray *tlist = [NSMutableArray arrayWithContentsOfFile:tfilename];
            [list addObjectsFromArray:tlist];
            NSString *gfilename = [plistPath1 stringByAppendingPathComponent:@"GoolinkDeviceList.plist"];
            NSMutableArray *glist = [NSMutableArray arrayWithContentsOfFile:gfilename];
            [list addObjectsFromArray:glist];
        }
            break;
    }
    return list;
}

@end
